package com.yingyi.user.service.impl;

import com.yingyi.basic.constant.Identity;
import com.yingyi.basic.service.impl.BaseServiceImpl;
import com.yingyi.org.domain.Employee;
import com.yingyi.org.mapper.EmployeeMapper;
import com.yingyi.user.domain.Logininfo;
import com.yingyi.user.domain.User;
import com.yingyi.user.mapper.LogininfoMapper;
import com.yingyi.user.mapper.UserMapper;
import com.yingyi.user.service.ILogininfoService;


import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * (t_logininfo)表服务层实现类
 *
 * @author Yingyi
 * @since 2022-07-27 22:31:25
 */
@Service
public class LogininfoServiceImpl extends BaseServiceImpl<Logininfo> implements ILogininfoService {

    @Autowired
    private LogininfoMapper logininfoMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private EmployeeMapper employeeMapper;
    /**
     * 对t_logininfo表中的记录作增删改操作需要同步到t_employee或t_user表中
     * @param logininfo
     */

    @Override
    @Transactional
    public void insert(Logininfo logininfo) {
        if(logininfo.getType() == Identity.EMPLOYEE){
            Employee employee = logininfo2Employee(logininfo);

            //employeeMapper中的insert方法会将
            employeeMapper.insert(employee);
        }else if(logininfo.getType() == Identity.USER){
            User user = logininfo2User(logininfo);
            userMapper.insert(user);
        }


    }

    @Override
    @Transactional
    public void update(Logininfo logininfo) {
        if(logininfo.getType() == Identity.EMPLOYEE){
            Employee employee = logininfo2Employee(logininfo);

            employeeMapper.update(employee);
        }else if(logininfo.getType() == Identity.USER){
            User user = logininfo2User(logininfo);
            userMapper.update(user);
        }
    }


    @Override
    @Transactional
    public void delete(Long id) {
        Logininfo logininfo = logininfoMapper.queryById(id);

        if(logininfo.getType() == Identity.EMPLOYEE){
            //Employee employee = logininfo2Employee(logininfo);

            Employee employee = employeeMapper.queryByLogininfoId(id);
            employeeMapper.delete(employee.getId());
        }else if(logininfo.getType() == Identity.USER){
            User user = userMapper.queryByLogininfoId(id);
            userMapper.delete(user.getId());
        }
    }


    @Override
    @Transactional
    public void patchDelete(List<Long> ids) {
        for (Long id : ids) {
            delete(id);
        }
    }

    private Employee logininfo2Employee(Logininfo logininfo) {
        Employee employee = new Employee();

        /**
         * 相同字段赋值
         */
        BeanUtils.copyProperties(logininfo, employee);

        /**
         * 不同字段赋值
         */
        employee.setState(logininfo.getDisable() ? 0 : 1);
        return employee;
    }

    private User logininfo2User(Logininfo logininfo) {
        User user = new User();

        /**
         * 相同字段赋值
         */
        BeanUtils.copyProperties(logininfo, user);

        /**
         * 不同字段赋值
         */
        user.setState(logininfo.getDisable() ? 0 : 1);
        return user;
    }
}
